package com.supervision.manage.controller.user

import com.fly.base.annotation.AuthPermission
import com.fly.base.common.DataResponse
import com.fly.base.controller.BaseController
import com.supervision.domain.bean.po.AdminSearchPO
import com.supervision.manage.service.AdminService
import com.supervision.manage.service.TokenService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

/**
 *  Created by whis on 2019-06-23
 */
@RestController
@RequestMapping("/manage/1/admin")
@Api(tags = ["管理员"])
class AdminController : BaseController() {

    @Autowired
    lateinit var adminService: AdminService

    @Autowired
    lateinit var tokenService: TokenService

    @RequestMapping("login", method = [(RequestMethod.POST)])
    @AuthPermission(needToken = false)
    @ApiOperation("管理员登录")
    fun login(@RequestParam("username") username: String,
              @RequestParam("password") password: String,
              @RequestParam("code") code: String,
              @RequestParam("uuid") uuid: String
    ): DataResponse {
        // TODO 验证验证码
        val data = adminService.login(username, password)
        return DataResponse.create().putAll(data)
    }

    @RequestMapping("getInfo", method = [(RequestMethod.GET)])
    @ApiOperation("获取用户信息")
    fun getInfo(): DataResponse {
        val userId = userContext!!.userId
        val data = adminService.getInfo(userId)
        return DataResponse.create().putAll(data)
    }

    @RequestMapping("add", method = [(RequestMethod.POST)])
    @ApiOperation("新增管理员")
    fun add(@RequestParam("username") username: String,
            @RequestParam("password") password: String,
            @RequestParam("name", defaultValue = "", required = false) name: String): DataResponse {
        adminService.addAdmin(username, password, name)
        return DataResponse.create()
    }

    @RequestMapping("update", method = [(RequestMethod.POST)])
    @ApiOperation("编辑管理员")
    fun update(@RequestParam("id") id: Long,
               @RequestParam("username") username: String,
               @RequestParam("name") name: String): DataResponse {
        adminService.updateAdmin(id, username, name)
        return DataResponse.create()
    }

    @RequestMapping("delete", method = [(RequestMethod.POST)])
    @ApiOperation("删除管理员")
    fun deleteAdmin(id: Long): DataResponse {
        adminService.deleteAdmin(id)
        return DataResponse.create()
    }

    @RequestMapping("list", method = [(RequestMethod.GET)])
    @ApiOperation("管理员列表")
    fun adminList(po: AdminSearchPO): DataResponse {
        val data = adminService.getAdminList(po)
        return DataResponse.create().putAll(data)
    }

    @RequestMapping("logout", method = [(RequestMethod.POST)])
    @ApiOperation("登出")
    fun logout(@RequestParam("token") token: String): DataResponse {
        tokenService.makeTokenInvalid(token)
        return DataResponse.create()
    }

    @RequestMapping("passwordReset", method = [(RequestMethod.POST)])
    @ApiOperation("重置密码")
    fun resetPassword(@RequestParam id: Long,
                      @RequestParam password: String): DataResponse {
        adminService.resetPassword(id, password)
        return DataResponse.create()
    }

    @RequestMapping("passwordUpdate", method = [(RequestMethod.POST)])
    @ApiOperation("修改密码")
    fun updatePassword(@RequestParam oldPwd: String,
                       @RequestParam newPwd: String): DataResponse {
        val userId = userContext!!.userId
        adminService.updatePassword(userId, oldPwd, newPwd)
        return DataResponse.create()
    }
}